package com.woniu.gateway.config;

import com.alibaba.csp.sentinel.adapter.gateway.sc.SentinelGatewayFilter;
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.DefaultBlockRequestHandler;
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
import jakarta.annotation.PostConstruct;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class GatewayConfiguration {

    @Bean
    public GlobalFilter sentinelGatewayFilter() {
        return (exchange, chain) -> {
            return new SentinelGatewayFilter().filter(exchange, chain);
        };
    }

    @PostConstruct
    public void doInit() {
        GatewayCallbackManager.setBlockHandler(new DefaultBlockRequestHandler());
        GatewayCallbackManager.setRequestOriginParser(exchange -> "default");
    }
}

